﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using YouHP.Utility;
using YouHP.BLL;
using System.Web;
using YouHP.Model;

namespace YouHP.Pages.User
{
    public class ActionPost : BasePage
    {
        protected List<UserInfo> FriendDataList;
        protected Dictionary<int, string> checkedUserDictionary;
        protected int PageIndex;
        protected int DataCount;
        protected int PageCount;
        protected string PagingString = "";
        protected const int StateIsFriend = 2;

        public override void InitPage()
        {
            checkedUserDictionary = new Dictionary<int,string>();

            base.InitPage();
            
            base.ShowPage();
            HandleGetFriends();
        }

        protected override void ShowPage()
        {
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost)
            {
                HandlePost();
            }
        }

        protected LogTopicInfo LogTopicData;

        protected void HandleCommon()
        {
            this.AddLocation("添加表情交互", "");
            this.SetTitle("添加表情交互");
        }

        protected void HandlePost()
        {
            bool succ = false;
            if (UserData.Id <= 0)
            {
                this.AddErrLine("请您登录");
                this.SetUrl("/User/Login.aspx");
                this.SetMetaRefresh();
                return;
            }

            if (ValidateForm())
            {
                foreach(KeyValuePair<int,string> item in checkedUserDictionary )
                {
                    ActionInfo info = new ActionInfo();
                    info.ActionType = RequestTools.GetFormInt("actiontype", 0);
                    info.Remark = RequestTools.GetFormString("msg").Trim();
                    info.ToUserId = item.Key;
                    info.ToUserName = item.Value;
                    info.FromUserId = UserData.Id;
                    info.FromUserName = UserData.UserName;
                    info.PostTime = DateTime.Now;
                    succ = ActionBLL.Instance.AddAction(info);
                }
            }

            if (succ)
            {
                this.AddMsgLine("操作成功");
            }
            else
            {
                this.AddErrLine("操作失败");
            }
            this.SetUrl("/User/ActionPost.aspx");
            this.SetMetaRefresh();
        }

        protected void HandleGetFriends()
        {
            PageIndex = RequestTools.GetQueryInt("page", 1);

            FriendDataList = FriendBLL.Instance.GetFriendList(20, PageIndex, UserData.Id, StateIsFriend, out DataCount);
            foreach (UserInfo userInfo in FriendDataList)
            {
                if (!checkedUserDictionary.ContainsKey(userInfo.Id))
                {
                    checkedUserDictionary.Add(userInfo.Id, userInfo.UserName);
                }
            }
            PageCount = Utils.GetPageCount(DataCount, 20);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PagingString = this.GetPagingString(
                PageIndex,
                PageCount,
                "/User/FriendList.aspx" + query, "page");
        }

        bool ValidateForm()
        {
            ActionInfo info = new ActionInfo();

            info.ActionType = RequestTools.GetFormInt("actiontype", 0);
            if (info.ActionType >= Constants.HugTypeCount)
            {
                this.AddErrLine("无效的表情");
                return false;
            }
            info.Remark = RequestTools.GetFormString("msg").Trim();
            if (info.Remark.Length < 2)
            {
                this.AddErrLine("信息长度不能小于 2 ");
                return false;
            }
            else if (info.Remark.Length > 1000)
            {
                this.AddErrLine("信息长度不能大于 1000 ");
                return false;
            }

            return true;
        }

    }
}
